package com.peas.platform.module.icims;

import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.junit.Test;

public class AttachmentRenameTestCase {

    @Test
    public void rename() {
        String[] ss = new String[]{"75516bfdb2110f0f56d4377b5e4bbfd2_957eb32f4c52987298b29a5dee524044_8.xlsx"};
        for (String s : ss) {
            String url = "http://icims.47.238.196.250.nip.io/icims-api/api/v1/attachment/listByField?field=original&values=" + s;
            String r = HttpUtil.get(url);
            JSONObject o = JSONUtil.parseObj(r);
            Assert.equals("000", o.getStr("code"), o.getStr("message"));
            JSONArray arr = o.getJSONArray("data");
            for (int i = 0; i < arr.size(); i++) {
                JSONObject d = arr.getJSONObject(i);
                d.set("original", "7a5dee524044" + String.format("%08d", i) + ".xlsx");
                String uri = "http://icims.47.238.196.250.nip.io/icims-api/api/v1/attachment/modify";
                HttpRequest req = HttpUtil.createRequest(Method.PUT, uri);
                req.header("userId", d.getStr("uploader"));
                req.body(JSONUtil.toJsonStr(d));
                HttpResponse resp = req.execute();
                JSONObject res = JSONUtil.parseObj(resp.body());
                Assert.equals("000", res.getStr("code"), res.getStr("message"));
            }
        }
    }

}
